package l1j.server.server.model;

import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;

public class ItemMakeByDoll extends TimerTask {
	private static Logger _log = Logger.getLogger(ItemMakeByDoll.class.getName());

	private final L1PcInstance _pc;
	private final int _itemId;
	public ItemMakeByDoll(final L1PcInstance pc , final int itemId) {
		this._pc = pc;
		this._itemId = itemId;
	}

	public void itemMake() {
		final L1ItemInstance temp = ItemTable.getInstance().createItem(this._itemId);
		if (temp != null) {
			if (this._pc.getInventory().checkAddItem(temp, 1) == L1Inventory.OK) {
				final L1ItemInstance item = this._pc.getInventory().storeItem(temp.getItemId(), 1);
				this._pc.sendPackets(new S_ServerMessage(403, item.getItem().getName())); // %0 を手に入れました
			}
		}
	}

	@Override
	public void run() {
		try {
			if (this._pc.isDead()) {
				return;
			}
			this.itemMake();
		} catch (final Throwable e) {
			_log.log(Level.WARNING, e.getLocalizedMessage(), e);
		}
	}
}
